(function($) {
    "use strict";
    
    Drupal.behaviors.uc_color_attribute = {
        attach: function() {
            $('div.colors-color').click(function() {
                  var nid = $(this).parent('div.colors-background').find('input[type=hidden]').val(),
                      ctx = $('#uc-product-add-to-cart-form-'+nid),
                      aid = $(this).attr('attrid'),
                      oid = $(this).attr('oid'),
                      msg = $('div.txtmsg',ctx),
                      title = $(this).attr('title');
                      
                  $('select[name=\'attributes['+aid+']\']',ctx).val(oid).trigger('change');
                  
                  if(msg.length > 0){
                          $(msg).html('<strong>Selected Color:</strong> '+title);
                  }else{
                          $('div.colors-background',ctx).before('<div class="txtmsg"><strong>Selected Colour:</strong> '+title+'</div>');
                  }                  
            });
        }
    }
})(jQuery);